! **************************************************************************************
!  Rob Crain (2010), Centre for Astrophysics and Superpcomputing, Swinburne, Australia
! **************************************************************************************
! Code to convert Adrian Jenkin's binary ICs into hdf5 ICs, at the same time converting 
! the particle IDs into the more familiar scheme of numbering by PartType
! **************************************************************************************

program main
  use hdf5_wrapper
  use myfiles
  use globals
  use particles
  use indexx
  implicit none

  ! Local variables
  integer*4          :: ifile
  character(len=16)  :: cfile
  character(len=512) :: infile
  character(len=512) :: outfile

  ! Perform start-up 
  call startup()

  ! Read parameters
  call read_parameters()

  ! Read in the initial conditions
  call read_ic_header_binary()

  ! For each file, write out an analogous HDF5 file
  do ifile=0,header.numfilespersnapshot-1

     write(logunit,*)' -ifile = ',ifile
     write(cfile,*)ifile
     infile  = trim(adjustl(ICdir))//trim(adjustl(ICbase))//'.'//trim(adjustl(cfile))
     outfile = trim(adjustl(outdir))//trim(adjustl(ICbase))//'.'//trim(adjustl(cfile))//'.hdf5'
     
     ! Read in the data
     call read_ic_binary(infile)
     
     ! Write out as HDF5
     call write_new_ics(outfile)

  enddo


end program main

! *******************************************************************************
! *******************************************************************************
! *******************************************************************************

subroutine abort(message)
  use globals
  use numbers
  implicit none
  character(*), intent(in) :: message
  integer ierr

  ! Print message to log
  write(logunit,*)message

  ! Close logfiles
  close(logunit)

  stop
end subroutine abort

! *******************************************************************************

subroutine startup()
  use globals
  use myfiles
  implicit none
  integer*4 :: nargs,iargc

  ! Check that arguments are correct and read in parmfile
  nargs = iargc()
  if(nargs /= 1) call abort("usage: main paramfile")
  call getarg(1,paramfile)

  logunit = 6

  ! Say hello
  write(logunit,*)' --------------------------------------------------------------------------'
  write(logunit,*)' DumpHDF5 v0.1 :: Rob Crain, CAS, Swinburne'
  write(logunit,*)' --------------------------------------------------------------------------'
  write(logunit,*)
  call flush(logunit)

end subroutine startup


! *******************************************************************************

